﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SCMS
{
    public partial class SCMS : Form
    {
        [STAThread]

        public static void Main()
        {
            DAOManager.ConnectionString = @"Data Source=DIVINE\SQLExpress;initial catalog=SCMS;Integrated Security=True;Connect Timeout=30;";

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new SCMS());
        } 

        public SCMS()
        {
            InitializeComponent();
        }

        private void _toolstripExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        #region Inventory
        private Items mItems = new Items();
        private Int64 Syskey = GeneralUtility.GenerateId();


        #endregion

        private void _toolstripInventorycontrol_Click(object sender, EventArgs e)
        {
            _panelInventorycontrol.Visible = true;
            ClearInventoryScreen();
            _txtICItemID.Focus();
        }

        private void UpdateInventoryObject()
        {
            mItems.mItemsInfo.syskey = Syskey;
            mItems.mItemsInfo.itemID = _txtICItemID.Text;
            mItems.mItemsInfo.itemName = _txtICItemName.Text;
            mItems.mItemsInfo.itemPrice = Double.Parse(_txtICItemPrice.Text);
            mItems.mItemsInfo.itemDescription = _txtICItemDescription.Text;
            mItems.mItemsInfo.itemQuantity = Int32.Parse(_txtICItemQuantity.Text);
            mItems.mItemsInfo.supplierID = _txtICSupplierID.Text;
        }

        private void UpdateInventoryScreen()
        {
            _txtICItemID.Text = mItems.mItemsInfo.itemID;
            _txtICItemName.Text = mItems.mItemsInfo.itemName;
            _txtICItemPrice.Text = Convert.ToString(mItems.mItemsInfo.itemPrice);
            _txtICItemDescription.Text = mItems.mItemsInfo.itemDescription;
            _txtICItemQuantity.Text = Convert.ToString(mItems.mItemsInfo.itemQuantity);
            _txtICSupplierID.Text = mItems.mItemsInfo.supplierID;
        }

        private void ClearInventoryScreen()
        {
            _txtICItemID.Text = "";
            _txtICItemName.Text = "";
            _txtICItemPrice.Text = "";
            _txtICItemDescription.Text = "";
            _txtICItemQuantity.Text = "";
            _txtICSupplierID.Text = "";
        }

        private void _btnICSave_Click(object sender, EventArgs e)
        {
            Syskey = GeneralUtility.GenerateId();
            if (_txtICItemID.Text != "")
            {
                UpdateInventoryObject();
                if (mItems.Save())
                {
                    MessageBox.Show("Successfully Saved");
                }
                else
                {
                    MessageBox.Show("Data Cannot be Saved");
                }
            }
        }

        private void _btnICFind_Click(object sender, EventArgs e)
        {
            if (_txtICItemID.Text != "")
            {
                mItems.mItemsInfo.itemID = _txtICItemID.Text;
                if (mItems.Find() != null)
                {
                    UpdateInventoryScreen();
                }
                else
                {
                    MessageBox.Show("Can't find the record");
                }
            }
        }

        private void _btnICNext_Click(object sender, EventArgs e)
        {
            ClearInventoryScreen();
            _txtICItemID.Focus();
        }

        private void _btnICDelete_Click(object sender, EventArgs e)
        {
            if (_txtICItemID.Text != "")
            {
                if (mItems.Delete())
                {
                    MessageBox.Show("Successfully Deleted");
                }
                else
                {
                    MessageBox.Show("Record can't be deleted");
                }
            }
        }

        private void _btnICClear_Click(object sender, EventArgs e)
        {
            ClearInventoryScreen();
        }
    }
}
